##
## Hosted Game: Team Elimination
## D1st0rt, SSCE Hyperspace
## License: MIT/X11
## Created: 2009-02-22
## Updated: 2009-05-12
##
## Simple team elim module. Specify how many deaths with ?newgame #.
## Make sure HostedGame:ElimOnSpec = 1
##
import asss

chat = asss.get_interface(asss.I_CHAT)
game = asss.get_interface(asss.I_GAME)
hgcore = asss.get_interface(asss.I_HGCORE)
pd = asss.get_interface(asss.I_PLAYERDATA)
stats = asss.get_interface(asss.I_STATS)

class HG_Elim:
    iid = asss.I_HOSTEDGAME

    def OnPrepare(self, a, host, params):
        if params.isdigit() and int(params) > 0:
            a.hg_elim_deaths = int(params)
        chat.SendArenaMessage(a, "Team Elim to %d. Last team alive wins!" % a.hg_elim_deaths)
        return (1,)

    def OnStart(self, a, host):
        hgcore.EndInterval(a, asss.INTERVAL_RESET)
        game.GivePrize(a, asss.PRIZE_WARP, 1)
        game.ShipReset(a)
        return (1,)

    def OnGameOver(self, a, how):
        if how == asss.HGR_NONE_REMAINING:
            chat.SendArenaMessage(a, "Elim canceled, there is nobody left.")
        elif how == asss.HGR_NATURAL:
            data = {'mvp': 'unknown', 'mvpkills' : 0, 'team' : 0, 'a' : a}
            asss.for_each_player(lambda p : elim_mvp(p, data))
            chat.SendArenaMessage(a, "Team %d Wins!" % data['team'])
            chat.SendArenaMessage(a, "%s is the MVP with %d kills!" % (data['mvp'], data['mvpkills']))

    def OnCleanup(self, a):
        pass

    def OnTeamCreate(self, a, team):
        pass

    def OnTeamDestroy(self, a, team):
        pass

    def SizeOfTeamData(self):
        return 0

    def TeamInfoString(self, team, str):
        return str

def PlayerKill(a, killer, killed, bty, flags, pts, green):
    deaths = stats.GetStat(killed, asss.STAT_DEATHS, asss.INTERVAL_RESET)
    if deaths >= a.hg_elim_deaths:
        kills = stats.GetStat(killed, asss.STAT_KILLS, asss.INTERVAL_RESET)
        chat.SendArenaMessage(a, "%s eliminated (%d-%d)" % (killed.name, kills, deaths))
        game.SetFreqAndShip(killed, asss.SHIP_SPEC, a.specfreq)
    return (pts, green)

def elim_mvp(p, data):
    if p.arena == data['a']:
        kills = stats.GetStat(p, asss.STAT_KILLS, asss.INTERVAL_RESET)
        if kills > data['mvpkills']:
            data['mvpkills'] = kills
            data['mvp'] = p.name

        if p.ship != asss.SHIP_SPEC:
            data['team'] = p.freq

def mm_attach(a):
    try:
        a.hg_elim_myint = asss.reg_interface(HG_Elim(), a)
        a.hg_elim_cb1 = asss.reg_callback(asss.CB_KILL, PlayerKill, a)
        a.hg_elim_deaths = 5
        if hgcore.AttachGame(a, asss.I_HOSTEDGAME, "hg_elim") == 0:
            raise Exception("Failed to attach to HGCore")
    except:
        mm_detach(a)

def mm_detach(a):
    hgcore.DetachGame(a)
    for attr in ['myint', 'cb1', 'deaths']:
        try: delattr(a, 'hg_elim_' + attr)
        except: pass
